package com.xjscrm.console.web.tencentCos;

import com.xjscrm.console.biz.TencentCosService;
import com.xjscrm.console.vo.TencentCosUploadFileVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Api(value = "腾讯对象存储", tags = "腾讯对象存储")
@RestController
@RequestMapping(value = "/tencentCos")
public class TencentCosController {


    @Autowired
    private TencentCosService tencentCosService;
    @PostMapping("/uploadImage")
    @ApiOperation(value = "上传图片", notes = "上传图片", response = TencentCosUploadFileVo.class)
    public Object uploadImage(MultipartFile file){
        TencentCosUploadFileVo tencentCosUploadFileVo = new TencentCosUploadFileVo();
            String fileUrl =  tencentCosService.uploadFile(file);
            tencentCosUploadFileVo.setFileUrl(fileUrl);
            String fileTag = "[IMG]" + fileUrl + "[/IMG]";
            tencentCosUploadFileVo.setFileTag(fileTag);
            tencentCosUploadFileVo.setFilename(file.getOriginalFilename());
             return  tencentCosUploadFileVo;
    }


    @ApiOperation(value = "上传视频", notes = "上传视频", response = TencentCosUploadFileVo.class)
    @PostMapping("/uploadVideo")
    public Object uploadVideo(MultipartFile file){
        TencentCosUploadFileVo tencentCosUploadFileVo = new TencentCosUploadFileVo();
            String fileUrl =  tencentCosService.uploadFile(file); //上传视频
            tencentCosUploadFileVo.setFileUrl(fileUrl);
            String fileTag = "[VIDEO]" + fileUrl + "[/VIDEO]";
            tencentCosUploadFileVo.setFileTag(fileTag);
            tencentCosUploadFileVo.setFilename(file.getOriginalFilename());
            return tencentCosUploadFileVo;
    }

    @ApiOperation(value = "上传文件", notes = "上传文件", response = TencentCosUploadFileVo.class)
    @PostMapping("/uploadFile")
    public Object uploadFile(MultipartFile file){
        TencentCosUploadFileVo tencentCosUploadFileVo = new TencentCosUploadFileVo();
        String fileUrl =  tencentCosService.uploadFile(file); //上传视频
        tencentCosUploadFileVo.setFileUrl(fileUrl);
        String fileExtension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        String fileTag = "["+fileExtension.toUpperCase()+"]" + fileUrl + "[/"+fileExtension.toUpperCase()+"]";
        tencentCosUploadFileVo.setFileTag(fileTag);
        tencentCosUploadFileVo.setFilename(file.getOriginalFilename());
        return tencentCosUploadFileVo;
    }
}
